package com.gqj.elec.service.impl;

import com.gqj.common.utils.DateUtils;
import com.gqj.common.utils.uuid.HiultraUtil;
import com.gqj.elec.domain.ElecMaterialRepair;
import com.gqj.elec.domain.ElecMaterialRepairdetail;
import com.gqj.elec.mapper.ElecMaterialMapper;
import com.gqj.elec.mapper.ElecMaterialRepairMapper;
import com.gqj.elec.mapper.ElecMaterialRepairdetailMapper;
import com.gqj.elec.service.IElecMaterialRepairService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 物资检修Service业务层处理
 *
 * @author lxq
 * @date 2023-12-17
 */
@Service
public class ElecMaterialRepairServiceImpl implements IElecMaterialRepairService {
    @Autowired
    private ElecMaterialRepairMapper elecMaterialRepairMapper;
    @Autowired
    private ElecMaterialRepairdetailMapper elecMaterialRepairdetailMapper;
    @Autowired
    private ElecMaterialMapper elecMaterialMapper;

    /**
     * 查询物资检修
     *
     * @param repairId 物资检修主键
     * @return 物资检修
     */
    @Override
    public ElecMaterialRepair selectElecMaterialRepairByRepairId(Long repairId) {
        ElecMaterialRepair elecMaterialRepair = elecMaterialRepairMapper.selectElecMaterialRepairByRepairId(repairId);

        ElecMaterialRepairdetail elecMaterialRepairdetail =  new ElecMaterialRepairdetail();
        elecMaterialRepairdetail.setRepairId(repairId);

        List<ElecMaterialRepairdetail> elecMaterialList = elecMaterialRepairdetailMapper.selectElecMaterialRepairdetailList(elecMaterialRepairdetail);
        elecMaterialRepair.setElecMaterialList(elecMaterialList);
        return elecMaterialRepair;
    }

    /**
     * 查询物资检修列表
     *
     * @param elecMaterialRepair 物资检修
     * @return 物资检修
     */
    @Override
    public List<ElecMaterialRepair> selectElecMaterialRepairList(ElecMaterialRepair elecMaterialRepair) {
        return elecMaterialRepairMapper.selectElecMaterialRepairList(elecMaterialRepair);
    }

    /**
     * 新增物资检修
     *
     * @param elecMaterialRepair 物资检修
     * @return 结果
     */
    @Override
    @Transactional
    public int insertElecMaterialRepair(ElecMaterialRepair elecMaterialRepair) {
        Date nowDate = DateUtils.getNowDate();
        elecMaterialRepair.setBatchNumber("WX-" + HiultraUtil.getOrderNo());
        elecMaterialRepair.setMaterialCategory("0");
        elecMaterialRepair.setRepairStatus("0");
        elecMaterialRepair.setCreateTime(nowDate);
        int i = elecMaterialRepairMapper.insertElecMaterialRepair(elecMaterialRepair);

        List<ElecMaterialRepairdetail> elecMaterialList = elecMaterialRepair.getElecMaterialList();
        for (ElecMaterialRepairdetail material : elecMaterialList) {
            ElecMaterialRepairdetail elecMaterialRepairdetail = new ElecMaterialRepairdetail();
            elecMaterialRepairdetail.setRepairId(elecMaterialRepair.getRepairId());
            elecMaterialRepairdetail.setMaterialId(material.getMaterialId());
            elecMaterialRepairdetail.setCreateBy(elecMaterialRepair.getCreateBy());
            elecMaterialRepairdetail.setCreateTime(nowDate);
            elecMaterialRepairdetail.setRepairDate(elecMaterialRepair.getPlanRepairDate());
            elecMaterialRepairdetailMapper.insertElecMaterialRepairdetail(elecMaterialRepairdetail);
        }
        return i;
    }

    /**
     * 修改物资检修
     *
     * @param elecMaterialRepair 物资检修
     * @return 结果
     */
    @Override
    @Transactional
    public int updateElecMaterialRepair(ElecMaterialRepair elecMaterialRepair) {
        elecMaterialRepair.setUpdateTime(DateUtils.getNowDate());
        int i = elecMaterialRepairMapper.updateElecMaterialRepair(elecMaterialRepair);

        elecMaterialRepairdetailMapper.deleteElecMaterialRepairdetailByoutId(elecMaterialRepair.getRepairId());
        List<ElecMaterialRepairdetail> elecMaterialList = elecMaterialRepair.getElecMaterialList();
        for (ElecMaterialRepairdetail material : elecMaterialList) {
            ElecMaterialRepairdetail elecMaterialRepairdetail = new ElecMaterialRepairdetail();
            elecMaterialRepairdetail.setRepairId(elecMaterialRepair.getRepairId());
            elecMaterialRepairdetail.setMaterialId(material.getMaterialId());
            elecMaterialRepairdetail.setUpdateBy(elecMaterialRepair.getCreateBy());
            elecMaterialRepairdetail.setUpdateTime(elecMaterialRepair.getUpdateTime());
            elecMaterialRepairdetail.setRepairDate(elecMaterialRepair.getPlanRepairDate());
            elecMaterialRepairdetailMapper.insertElecMaterialRepairdetail(elecMaterialRepairdetail);
        }

        return i;
    }

    /**
     * 批量删除物资检修
     *
     * @param repairIds 需要删除的物资检修主键
     * @return 结果
     */
    @Override
    public int deleteElecMaterialRepairByRepairIds(Long[] repairIds) {
        return elecMaterialRepairMapper.deleteElecMaterialRepairByRepairIds(repairIds);
    }

    /**
     * 删除物资检修信息
     *
     * @param repairId 物资检修主键
     * @return 结果
     */
    @Override
    public int deleteElecMaterialRepairByRepairId(Long repairId) {
        return elecMaterialRepairMapper.deleteElecMaterialRepairByRepairId(repairId);
    }
}
